<template>
  <div class="home">
    <header class="hero">
      <div class="hero-content">
        <h1>算法分类与分析平台</h1>
        <p>探索经典算法，提升编程能力</p>
      </div>
    </header>

    <section class="intro-section">
      <h2>平台介绍</h2>
      <p>本平台提供了各种经典算法的详细介绍、实现和分析，帮助您深入理解算法原理和应用场景。</p>
    </section>

    <section class="algorithm-categories">
      <h2>算法分类</h2>
      <el-row class="category-cards">
        <el-col 
          v-for="type in algorithmTypes" 
          :key="type.typeId" 
          :xs="24" 
          :sm="12" 
          :md="8" 
          :lg="6">
          <div class="category-card" @click="navigateToAlgorithm(type.typeId)">
            <h3>{{ type.typeName }}</h3>
            <p>{{ type.description }}</p>
          </div>
        </el-col>
      </el-row>
    </section>

    <section class="video-section">
      <h2>最新教学视频</h2>
      <el-row :gutter="20" v-loading="videosLoading">
        <el-col 
          v-for="video in recentVideos" 
          :key="video.videoId" 
          :xs="24" 
          :sm="12" 
          :md="8">
          <el-card class="video-card" shadow="hover">
            <div class="video-thumbnail">
              <el-image 
                :src="generateThumbnail(video.videoUrl)"
                fit="cover">
                <template #error>
                  <div class="video-placeholder">
                    <el-icon><VideoCamera /></el-icon>
                  </div>
                </template>
              </el-image>
              <span class="video-duration">{{ formatDuration(video.duration) }}</span>
            </div>
            <div class="video-info">
              <h4>{{ video.videoTitle }}</h4>
              <p>{{ video.videoDescription }}</p>
              <el-button type="primary" link @click="watchVideo(video)">
                观看视频
              </el-button>
            </div>
          </el-card>
        </el-col>
      </el-row>
      
      <!-- 添加分页组件 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="page.current"
          v-model:page-size="page.size"
          :page-sizes="[6, 12, 18, 24]"
          :total="page.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </section>

    <!-- 视频播放对话框 -->
    <el-dialog
      v-model="videoDialogVisible"
      :title="currentVideo?.videoTitle"
      width="80%"
      class="video-dialog"
      :before-close="handleCloseVideo">
      <div class="video-player">
        <iframe
          v-if="currentVideo"
          :src="currentVideo.videoUrl"
          frameborder="0"
          allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
          allowfullscreen>
        </iframe>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { VideoCamera } from '@element-plus/icons-vue'
import axios from 'axios'

export default {
  name: 'Home',
  components: {
    VideoCamera
  },
  data() {
    return {
      algorithmTypes: [],
      recentVideos: [],
      videosLoading: false,
      videoDialogVisible: false,
      currentVideo: null,
      loading: false,
      // 添加分页相关数据
      page: {
        current: 1,
        size: 6,
        total: 0
      }
    }
  },
  created() {
    this.fetchAlgorithmTypes()
    this.fetchRecentVideos()
  },
  methods: {
    async fetchAlgorithmTypes() {
      try {
        this.loading = true
        const response = await axios.get('/api/algorithm-types')
        if (response.data.code === 200) {
          this.algorithmTypes = response.data.data
        } else {
          this.$message.warning('获取算法类型失败')
        }
      } catch (error) {
        console.error('获取算法类型失败:', error)
        this.$message.error('获取算法类型失败，请稍后重试')
      } finally {
        this.loading = false
      }
    },
    async fetchRecentVideos() {
      try {
        this.videosLoading = true
        const params = {
          page: this.page.current,
          size: this.page.size
        }
        
        const response = await axios.get('/api/teaching-videos/page', { params })
        
        if (response.data.code === 200) {
          const { records, total, current, size } = response.data.data
          this.recentVideos = records
          this.page.total = total
          this.page.current = current
          this.page.size = size
        } else {
          this.$message.warning('获取视频列表失败')
        }
      } catch (error) {
        console.error('获取视频列表失败:', error)
        this.$message.error('获取视频列表失败，请稍后重试')
      } finally {
        this.videosLoading = false
      }
    },
    navigateToAlgorithm(typeId) {
      this.$router.push(`/algorithm/${typeId}`)
    },
    generateThumbnail(videoUrl) {
      // 这里可以根据实际情况生成缩略图URL
      return `${videoUrl}/thumbnail`
    },
    formatDuration(seconds) {
      const minutes = Math.floor(seconds / 60)
      const remainingSeconds = seconds % 60
      return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`
    },
    watchVideo(video) {
      this.currentVideo = video
      this.videoDialogVisible = true
    },
    handleCloseVideo(done) {
      this.currentVideo = null
      done()
    },
    // 处理分页大小改变
    handleSizeChange(val) {
      this.page.size = val
      this.fetchRecentVideos()
    },
    // 处理页码改变
    handleCurrentChange(val) {
      this.page.current = val
      this.fetchRecentVideos()
    }
  }
}
</script>

<style scoped>
.home {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.hero {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 60px 20px;
  border-radius: 10px;
  margin-bottom: 40px;
  text-align: center;
}

.hero h1 {
  font-size: 2.5em;
  margin-bottom: 15px;
}

.hero p {
  font-size: 1.2em;
  opacity: 0.9;
}

section {
  margin-bottom: 40px;
}

h2 {
  color: #2c3e50;
  margin-bottom: 20px;
  font-size: 1.8em;
  border-bottom: 2px solid #eee;
  padding-bottom: 10px;
}

.category-cards {
  margin: 0 -10px;
}

.category-card {
  background-color: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  cursor: pointer;
  margin: 10px;
  height: calc(100% - 20px);
}

.category-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.category-card h3 {
  color: #34495e;
  margin-bottom: 10px;
}

.category-card p {
  color: #666;
  font-size: 0.9em;
  line-height: 1.6;
}

.intro-section {
  background-color: #f8f9fa;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 30px;
}

.intro-section p {
  line-height: 1.6;
  color: #2c3e50;
}

.video-card {
  margin-bottom: 20px;
}

.video-thumbnail {
  position: relative;
  width: 100%;
  padding-top: 56.25%; /* 16:9 aspect ratio */
}

.video-thumbnail .el-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 4px;
}

.video-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background-color: #f5f7fa;
  color: #909399;
  font-size: 24px;
}

.video-duration {
  position: absolute;
  bottom: 8px;
  right: 8px;
  background-color: rgba(0, 0, 0, 0.75);
  color: white;
  padding: 2px 4px;
  border-radius: 2px;
  font-size: 12px;
}

.video-info {
  padding: 12px 0;
}

.video-info h4 {
  margin: 0 0 8px;
  font-size: 16px;
  font-weight: 500;
  color: #2c3e50;
}

.video-info p {
  margin: 0 0 12px;
  font-size: 14px;
  color: #606266;
  line-height: 1.4;
}

.video-dialog :deep(.el-dialog__body) {
  padding: 0;
}

.video-player {
  position: relative;
  width: 100%;
  padding-top: 56.25%;
}

.video-player iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

/* 添加分页容器样式 */
.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  padding: 20px 0;
}

@media (max-width: 768px) {
  .hero h1 {
    font-size: 2em;
  }
  
  .hero p {
    font-size: 1em;
  }
  
  .category-card {
    margin-bottom: 15px;
  }
}
</style>